package com.fly.estore.controller;

import com.fly.estore.domain.MemberInfo;
import com.fly.estore.domain.MemberLevel;
import com.fly.estore.domain.MemberPoint;
import com.fly.estore.domain.SaleInfo;
import com.fly.estore.framework.common.AjaxResult;
import com.fly.estore.framework.session.AccountSession;
import com.fly.estore.framework.session.support.SessionInfo;
import com.fly.estore.framework.util.CommonUtil;
import com.fly.estore.service.MemberLevelService;
import com.fly.estore.service.MemberService;
import com.fly.estore.service.PointService;
import com.fly.estore.service.SaleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by Fly on 2016/4/5.
 */
@Controller
@RequestMapping("/member")
public class MemberController {

    @Autowired
    MemberService memberService;
    @Autowired
    MemberLevelService memberLevelService;
    @Autowired
    PointService pointService;
    @Autowired
    SaleService saleService;

    @RequestMapping("/memberList")
    public String toMember(
            ModelMap model
    ){
        List<MemberInfo> memberList = memberService.queryMemberList("");
        model.addAttribute("dataList", memberList);
        return "/member/memberList";
    }

    @RequestMapping("/memberInfo")
    public String toMemberInfo(
            @RequestParam(value = "memberId", required = false, defaultValue = "")String memberId,
            ModelMap model
    ){
        MemberInfo member = new MemberInfo();
        if (StringUtils.isNotBlank(memberId)){
            //根据memberId获取会员信息
            member = memberService.getById(memberId);
        }
        model.addAttribute("member", member);
        //会员等级列表
        List<MemberLevel> levelList = memberLevelService.getMemberLevelList();
        model.addAttribute("levelList", levelList);
        //会员积分
        int totalPoint = pointService.getMemberPoint(memberId);
        model.addAttribute("point", totalPoint);
        return "/member/memberInfo";
    }

    @RequestMapping("/saveMemberInfo")
    @ResponseBody
    public AjaxResult saveMemberInfo(
            @RequestParam(value = "memberId", required = false, defaultValue = "")String memberId,
            @RequestParam(value = "memberNo", required = false, defaultValue = "")String memberNo,
            @RequestParam(value = "memberName", required = false, defaultValue = "")String memberName,
            @RequestParam(value = "gender", required = false, defaultValue = "")String gender,
            @RequestParam(value = "birthday", required = false, defaultValue = "")String birthday,
            @RequestParam(value = "mobile", required = false, defaultValue = "")String mobile,
            @RequestParam(value = "email", required = false, defaultValue = "")String email,
            @RequestParam(value = "qq", required = false, defaultValue = "")String qq,
            @RequestParam(value = "address", required = false, defaultValue = "")String address,
            @RequestParam(value = "memberLevelId", required = false, defaultValue = "")String level,
            @SessionInfo AccountSession accountSession
            )
    {
        MemberInfo memberInfo = new MemberInfo();
        memberInfo.setMemberId(memberId);
        memberInfo.setMemberNo(memberNo);
        memberInfo.setMemberName(memberName);
        memberInfo.setGender(gender);
        memberInfo.setBirthday(birthday);
        memberInfo.setMobile(mobile);
        memberInfo.setEmail(email);
        memberInfo.setQq(qq);
        memberInfo.setAddress(address);
        memberInfo.setLevel(level);
        memberInfo.setCreateTime(CommonUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        memberInfo.setCreateId(accountSession.getAccountId());
        memberInfo.setLastUpdateId(accountSession.getAccountId());
        memberInfo.setLastUpdateTime(CommonUtil.dateToStr(new Date(), "yyyy-MM-dd HH:mm:ss"));
        int update = memberService.saveMemberInfo(memberInfo);

        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return ajaxResult;
    }

    @RequestMapping("/deleteMember")
    @ResponseBody
    public AjaxResult deleteMember(
            @RequestParam("memberId")String memberId
    ){
        int update = memberService.deleteById(memberId);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return ajaxResult;
    }

    //--------------------------------------会员等级-----------------------------------------------------------------//
    @RequestMapping("/level/list")
    public String memberLevelList(
            ModelMap model
    ){
        List<MemberLevel> memberLevelList = memberLevelService.getMemberLevelList();
        model.addAttribute("levelList", memberLevelList);
        return "/member/level/list";
    }

    @RequestMapping("/level/detail")
    public String memberLevelDetail(
            @RequestParam(value = "memberLevelId", required = false, defaultValue = "")String memberLevelId,
            ModelMap model
    ){
        MemberLevel memberLevel = memberLevelService.getById(memberLevelId);
        model.addAttribute("level", memberLevel);
        return "/member/level/detail";
    }

    @RequestMapping("/level/save")
    @ResponseBody
    public AjaxResult saveMemberLevel(
            @RequestParam(value = "memberLevelId", required = false, defaultValue = "")String memberLevelId,
            @RequestParam(value = "levelName", required = false, defaultValue = "")String levelName,
            @RequestParam(value = "levelIcon", required = false, defaultValue = "")String levelIcon,
            @RequestParam(value = "discount", required = false, defaultValue = "0")float discount,
            @SessionInfo AccountSession accountSession
    ){
        MemberLevel memberLevel = new MemberLevel();
        memberLevel.setMemberLevelId(memberLevelId);
        memberLevel.setLevelName(levelName);
        memberLevel.setLevelIcon(levelIcon);
        memberLevel.setDiscount(discount);
        memberLevel.setCreateId(accountSession.getAccountId());
        memberLevel.setCreateTime(CommonUtil.currentTime());
        memberLevel.setLastUpdateId(accountSession.getAccountId());
        memberLevel.setLastUpdateTime(CommonUtil.currentTime());
        int update = memberLevelService.saveMemberLevel(memberLevel);

        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    @RequestMapping("/level/delete")
    @ResponseBody
    public AjaxResult deleteMemberLevel(
            @RequestParam("memberLevelId")String memberLevelId
    ){
        int update = memberLevelService.deleteById(memberLevelId);
        AjaxResult result = new AjaxResult();
        result.setStatus(update > 0 ? AjaxResult.STATUS_SUCCESS : AjaxResult.STATUS_ERROR);
        return result;
    }

    //会员积分
    @RequestMapping("/point/list")
    public String memberPointList(
            @RequestParam(value = "memberId", required = false, defaultValue = "")String memberId,
            ModelMap model
    ){
        List<MemberPoint> pointList = pointService.getMemberPointByMemberId(memberId);
        model.addAttribute("pointList", pointList);
        model.addAttribute("memberId", memberId);
        return "/member/memberPoint";
    }

    //会员历史订单
    @RequestMapping("/order/list")
    public String memberOrderList(
            @RequestParam(value = "memberId", required = false, defaultValue = "")String memberId,
            ModelMap model
    ){
        List<SaleInfo> saleList = saleService.getSaleListByMemberId(memberId);
        model.addAttribute("saleList", saleList);
        model.addAttribute("memberId", memberId);
        return "/member/memberOrder";
    }

}
